import unittest, requests
import random
import logging
from common.read_json_util import assert_api
import app
from api.p2pApi import LoginApi
import time
from bs4 import BeautifulSoup

from common.three_util import third_request


class TestLogin(unittest.TestCase):
    real_name = "是是是"
    card_id = ""
    def setUp(self) -> None:
        # 接口类的实例化,session的实例化
        self.login_api = LoginApi()
        self.session = requests.Session()

    def tearDown(self) -> None:
        # 关闭session
        self.session.close()

    def test01_get_img_code_random_float_success(self):
        # 获取随机小数
        r = random.random()
        response = self.login_api.get_img_code(self.session,r)
        logging.info("response = {}".format(response.status_code))
        self.assertEqual(200,response.status_code)
    def test02_get_img_code_random_int_success(self):
        r = random.randint(1,9999)
        response = self.login_api.get_img_code(self.session,r)
        logging.info("response = {}".format(response.status_code))
        self.assertEqual(200,response.status_code)
    def test03_get_img_code_null_false(self):
        r = ""
        response = self.login_api.get_img_code(self.session,r)
        logging.info("response = {}".format(response.status_code))
        self.assertEqual(404, response.status_code)
    def test04_get_img_code_random_letter_false(self):
        # 获取随机字母
        r = "".join(random.sample("jdededsjswsw",8))
        print("join后的",r)
        response = self.login_api.get_img_code(self.session, r)
        logging.info("response = {}".format(response.status_code))
        self.assertEqual(400, response.status_code)

        # 参数正确,获取短信验证码成功
    def test05_get_sms_success(self):
        # 获取图片验证码
        r = random.random()
        response = self.login_api.get_img_code(self.session,r)
        logging.info("response = {}".format(response.status_code))
        self.assertEqual(200, response.status_code)
        # 获取短信验证码
        response = self.login_api.get_sms_code(self.session,app.phone1,app.img_code)
        logging.info("get_sms_code_response = {}".format(response.json()))
        assert_api(self,response,200,200,"短信发送成功")
        # 验证码错误,获取短信验证码失败
    def test05_get_sms_code_false(self):
        r = random.randint(10000,999999)
        response = self.login_api.get_img_code(self.session,r)
        logging.info("response={}".format(response.status_code))
        self.assertEqual(200,response.status_code)
        response = self.login_api.get_sms_code(self.session,app.phone1,img_code=1234)
        logging.info("get_sms_code_response = {}".format(response.json()))
        assert_api(self, response, 200, 100, "图片验证码错误")
        # 手机号为空获取验证码失败
    def test06_get_sms_code_phone_null_false(self):
        r = random.random()
        response = self.login_api.get_img_code(self.session,r)
        logging.info("response={}".format(response.status_code))
        response = self.login_api.get_sms_code(self.session,"",app.img_code)
        logging.info("get_sms_code_response = {}".format(response.json()))
        self.assertEqual(200,response.status_code)
        self.assertEqual(100,response.json().get("status"))
        # 验证码为空,获取短信验证码失败
    def test07_get_sms_code_img_code_null_false(self):
        r = random.random()
        response = self.login_api.get_img_code(self.session, r)
        logging.info("response={}".format(response.status_code))
        response = self.login_api.get_sms_code(self.session,app.phone1,"")
        logging.info("response={}".format(response.status_code))
        assert_api(self,response,200,100,"图片验证码错误")
        # 未请求图片验证码
    def test08_get_sms_code_no_img_false(self):
        response = self.login_api.get_sms_code(self.session,app.phone1,app.img_code)
        logging.info("response={}".format(response.status_code))
        assert_api(self, response, 200, 100, "图片验证码错误")
    # 注册接口
    def test09_must_register_success(self):
        r = random.random()
        response = self.login_api.get_img_code(self.session,r)
        logging.info(f"图片response为{response.status_code}")
        response = self.login_api.get_sms_code(self.session,app.phone5,app.img_code)
        logging.info(f"短信response为{response.status_code}")
        response = self.login_api.register_api(self.session,app.phone5,app.pwd,app.img_code,app.sms_code,dy_server="on",invite="")
        logging.info("注册response为{}".format(response.status_code))
        assert_api(self,response,200,200,"注册成功")
    def test10_all_register_success(self):
        r = random.random()
        response = self.login_api.get_img_code(self.session, r)
        logging.info(f"图片response为{response.status_code}")
        response = self.login_api.get_sms_code(self.session, app.phone4, app.img_code)
        logging.info(f"短信response为{response.status_code}")
        response = self.login_api.register_api(self.session, app.phone4, app.pwd, app.img_code,
                                               app.sms_code, dy_server="on", invite="13012125478")
        logging.info("注册response为{}".format(response.status_code))
        assert_api(self, response, 200, 200, "注册成功")
    def test11_phone_exist_false(self):
        r = random.random()
        response = self.login_api.get_img_code(self.session, r)
        logging.info(f"图片response为{response.status_code}")
        response = self.login_api.get_sms_code(self.session, app.phone3, app.img_code)
        logging.info(f"短信response为{response.status_code}")
        response = self.login_api.register_api(self.session, app.phone3, app.pwd, app.img_code,
                                               app.sms_code, dy_server="on", invite="13012125478")
        logging.info("注册response为{}".format(response.status_code))
        assert_api(self, response, 200, 100, "手机已存在!")
    def test12_password_null_false(self):
        r = random.random()
        response = self.login_api.get_img_code(self.session,r)
        logging.info(f"图片response为{response.status_code}")
        response = self.login_api.get_sms_code(self.session,app.phone3,app.img_code)
        logging.info(f"短信response为{response.status_code}")
        response = self.login_api.register_api(self.session, app.phone3, "", app.img_code,
                                               app.sms_code, dy_server="on", invite="13012125478")
        assert_api(self, response, 200, 100, "密码不能为空")
    # 用户名密码正确,登录成功
    def test13_login_success(self):
        response = self.login_api.login_api(self.session,app.phone2,app.pwd)
        logging.info("response={}".format(response.status_code))
        assert_api(self,response,200,200,"登录成功")
    def test14_login_not_null_false(self):
        # 定义测试数据,调用API方法发送请求
        response = self.login_api.login_api(self.session,phone="18276487643",password="test123")
        logging.info("response={}".format(response.status_code))
        # 对响应结果进行断言
        assert_api(self, response, 200, 100, "用户不存在")
        # 密码为空
    def test15_login_pwd_null_false(self):
        response = self.login_api.login_api(self.session,app.phone2,"")
        logging.info("response={}".format(response.json().get("description")))
        assert_api(self, response, 200, 100, "密码不能为空")
    def test16_login_pwd_false(self):
        # 密码错误一次
        response = self.login_api.login_api(self.session, app.phone4, "9999")
        logging.info("response={}".format(response.json().get("description")))
        assert_api(self, response, 200, 100, "密码错误1次,达到3次将锁定账户")
        # 密码错误两次
        response = self.login_api.login_api(self.session, app.phone4, "9999")
        logging.info("response={}".format(response.status_code))
        assert_api(self, response, 200, 100, "密码错误2次,达到3次将锁定账户")
        # 密码错误三次
        response = self.login_api.login_api(self.session, app.phone4, "9999")
        logging.info("response={}".format(response.status_code))
        assert_api(self, response, 200, 100, "由于连续输入错误密码达到上限，账号已被锁定，请于1.0分钟后重新登录")
        # 一分钟之内登录失败
        response = self.login_api.login_api(self.session,app.phone4,password="test123")
        logging.info("response={}".format(response.json().get("description")))
        assert_api(self, response, 200, 100, "由于连续输入错误密码达到上限，账号已被锁定，请于1.0分钟后重新登录")
        # 超过一分钟登录成功
        time.sleep(65)
        response = self.login_api.login_api(self.session,app.phone4,password="test123")
        logging.info("response={}".format(response.json().get("description")))
        assert_api(self, response, 200, 200, "登录成功")
    # 认证测试用例  # 定义认证的测试方法
    def test21_real_name_success(self):
        response = self.login_api.login_api(self.session, app.phone5, password="test123")
        logging.info("response={}".format(response))
        assert_api(self, response, 200, 200, "登录成功")
        response = self.login_api.real_name_api(self.session,"对对对","632525199008078545")
        logging.info("返回的状态码{}".format(response.json().get("status")))
        # assert_api(self, response, 200, 200, "提交成功!")
        # self.assertEqual("632****545",response.json().get("data").get("card_id"))
    def test22_realname_name_null(self):
        response = self.login_api.login_api(self.session, app.phone2, app.pwd)
        logging.info("response={}".format(response.status_code))
        # assert_api(self, response, 200, 200, "登录成功")
        response = self.login_api.real_name_api(self.session, "", "110101199003075293")
        logging.info("response={}".format(response.json().get("description")))
        assert_api(self, response, 200, 100, "姓名不能为空")
    def test23_realname_member_null_false(self):
        response = self.login_api.login_api(self.session, app.phone2, app.pwd)
        logging.info("response={}".format(response.status_code))
        assert_api(self, response, 200, 200, "登录成功")
        response = self.login_api.real_name_api(self.session, "是是是", "")
        logging.info("{}".format(response.json().get("description")))
        assert_api(self, response, 200, 100, "身份证号不能为空")
    def test24_realname_not_login_false(self):
        response = self.login_api.real_name_api(self.session, "是是是", "110101199003079892")
        logging.info("{}".format(response.json().get("description")))
        self.assertEqual(200,response.status_code)
        # 已注册过的
    def test25_member_use_false(self):
        response = self.login_api.login_api(self.session, app.phone2, app.pwd)
        logging.info("response={}".format(response))
        assert_api(self, response, 200, 200, "登录成功")
        response = self.login_api.real_name_api(self.session, "是是是", "110101199003078195")
        logging.info("返回的状态码{}".format(response.json()))
        assert_api(self, response, 200, 100, "已注册过")
        # 获取认证信息
    def test26_get_approve(self):
        response = self.login_api.login_api(self.session, app.phone2, app.pwd)
        logging.info("response={}".format(response))
        assert_api(self, response, 200, 200, "登录成功")
        # 获取认证信息
        response = self.login_api.get_approve_api(self.session)
        logging.info("response={}".format(response.json))
        self.assertEqual(200,response.status_code)
        self.assertEqual("330****334",response.json().get("data").get("card_id"))
        # 请求开户
    def test27_get_trust(self):
        response = self.login_api.login_api(self.session, app.phone2, app.pwd)
        logging.info("response={}".format(response))
        assert_api(self, response, 200, 200, "登录成功")
        response = self.login_api.get_register_trust(self.session)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(200,response.json().get("status"))
        # 第三方
    def test28_three_trust(self):
        response = self.login_api.login_api(self.session, app.phone2, app.pwd)
        logging.info("response={}".format(response))
        assert_api(self, response, 200, 200, "登录成功")
        response = self.login_api.get_register_trust(self.session)
        print(response.json())
        # 第三方开户,准备数据
        form_data = response.json().get("description").get("form")
        response = third_request(form_data)
        logging.info("response文本为{}".format(response.text))
    # 获取充值验证码
    def test29_get_verify_code(self):
        r = random.random()
        response = self.login_api.get_verify_code(self.session,r)
        self.assertEqual(200,response.status_code)
    # 充值
    def test30_recharge_success(self):
        response = self.login_api.login_api(self.session,app.phone2, app.pwd)
        logging.info("response={}".format(response))
        assert_api(self, response, 200, 200, "登录成功")
        r = random.random()
        response = self.login_api.get_verify_code(self.session, r)
        self.assertEqual(200, response.status_code)
        response = self.login_api.recharge_api(self.session,amount=1000,valicode=8888)
        logging.info("response为{}".format(response.json()))
        self.assertEqual(200,response.status_code)
        self.assertEqual(200,response.json().get("status"))
        # 第三方充值请求
    def test31_three_recharge(self):
        response1 = self.login_api.login_api(self.session, app.phone2, app.pwd)
        r = random.random()
        response2 = self.login_api.get_verify_code(self.session, r)
        response3 = self.login_api.recharge_api(self.session,amount=1000,valicode=8888)
        form_data = response3.json().get("description").get("form")
        info = third_request(form_data)
        logging.info("response = {}".format(info.text))
        # 充值金额超过最大投资金额
    def test32_recharge_more_than_must(self):
        response = self.login_api.login_api(self.session, app.phone2, app.pwd)
        logging.info("response={}".format(response))
        assert_api(self, response, 200, 200, "登录成功")
        r = random.random()
        response = self.login_api.get_verify_code(self.session, r)
        self.assertEqual(200, response.status_code)
        response = self.login_api.recharge_api(self.session, amount=1000000000000, valicode=8888)
        logging.info("response为{}".format(response.json()))
        self.assertEqual(200, response.status_code)
        self.assertEqual(100, response.json().get("status"))
    # 获取投资详细





























